const classExtend = require('./classExtend.js');
const instancesExtend = require('./instancesExtend.js');

const Base = {
    getAttr: function(sequelize, DataTypes) {
        return {
            creator: {
                field: "create_by",
                type: DataTypes.STRING(50),
                allowNull: false,
                canUpdate: false,
                defaultValue: "SYS",
                comment: "创建人"
            },
            cretaeDate: {
                type: DataTypes.DATE,
                field: "create_time",
                allowNull: false,
                canUpdate: false,
                defaultValue: DataTypes.NOW,
                comment: "创建时间"
            },
            modifier: {
                field: "update_by",
                type: DataTypes.STRING(50),
                allowNull: false,
                defaultValue: "SYS",
                comment: "修改人"
            },
            lastUpdatedDate: {
                type: DataTypes.DATE,
                field: "update_time",
                allowNull: false,
                defaultValue: DataTypes.NOW,
                comment: "最后修改时间"
            },
            isEnable: {
                field: "del_flag",
                type: DataTypes.INTEGER,
                allowNull: false,
                defaultValue: 0,
                comment: "是否删除 0 = 否 1= 是"
            },
            version: {
                field: "version",
                type: DataTypes.INTEGER,
                allowNull: false,
                defaultValue: 0,
                version: true,
                comment: "版本号"
            }
        };
    },
    extendModel: function(model, attr) {
        // 附件信息对象
        const modelExt = {
            pkName: '',
            // 获得定义的所有属性列
            akNameArray: [],
            // 所有可以修改的属性名称
            updates: [],
            // 所有的引用列属性名称
            references: []
        };


        var len = 0;
        Object.keys(attr).forEach(function(item) {
            len++;
            if (!attr[item].primaryKey) {
                if ((attr[item].canUpdate == null || attr[item].canUpdate) && (item != "version")) {
                    modelExt.updates.push(item);
                }
            } else {
                modelExt.pkName = item;
            }
            if (attr[item].unique) {
                modelExt.akNameArray.push(item);
            }
            if (attr[item].references) {
                modelExt.references.push(attr[item].references);
            }
        });
        attr.len = len;

        // 获得定义的属性列
        model.defineAtt = function() {
            return attr;
        };
        // 获得定义主键列
        model.getPkName = function() {
            return modelExt.pkName;
        };
        //model.defineAtt();
        // 增加模型类的扩展方法
        classExtend.extendModel(model, attr, modelExt);
        // 实例方法
        instancesExtend.extendModel(model, attr, modelExt);
    }
};

module.exports = Base;